package com.wft;

import java.util.Arrays;

public class Solution821 {
    public static void main(String[] args) {
//        String S = "loveleetcode";
//        char c = 'e';
        String S = "abaabbbbbab";
        char c = 'a';
        int[] result = shortestToChar(S, c);
        System.out.println(Arrays.toString(result));
    }

    public static int[] shortestToChar(String S, char C) {
        int[] result = new int[S.length()];
        char[] chars = S.toCharArray();
        int cIndex = 0;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == C) {
                result[i] = 0;
                if (cIndex != 0) {
                    for (int j = cIndex; j < i; j++) {
                        if (result[j] > i - j) {
                            result[j] = i - j;
                        }
                    }
                }
                cIndex = i + 1;
            } else {
                if (cIndex == 0) {
                    for (int j = cIndex; j <= i; j++) {
                        result[j] = result[j] + 1;
                    }
                } else {
                    for (int j = cIndex; j <= i; j++) {
                        result[j] = j - cIndex + 1;
                    }
                }
            }
        }
        return result;
    }

    /**
     * 3    5   6   11
     * lov e l e e tcod e
     * i    cIndex
     * 0    0       1
     * 1    0       2    1
     * 2    0       3    2   1
     * 3    2       3    2   1   0
     * 4    2       3    2   1   0   1
     * 5    4       3    2   1   0   1  0
     * 6    5       3    2   1   0   1  0  0
     * 7    5       3    2   1   0   1  0  0  1
     * 7    5       3    2   1   0   1  0  0  2  1
     */
}
